GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)
APP_MAIN_DIR=cmd
# 新增：定义包含 wire.go 的所有包目录
WIRE_DIRS=app cmd  # 这里根据实际拆分的包目录调整

.PHONY: run
# run
run:
	cd $(APP_MAIN_DIR) && go run main.go wire_gen.go app.go

.PHONY: build
# 自动根据平台编译二进制文件
build:
	mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./...

.PHONY: generate
# 生成应用所需的文件
generate:
	go mod tidy
	go get github.com/google/wire/cmd/wire@latest
	go generate ./...

.PHONY: wire
# wire：为所有包含 wire.go 的目录生成代码
wire:
	@for dir in $(WIRE_DIRS); do \
		if [ -f "$$dir/wire.go" ]; then \
			echo "Generating wire for $$dir..."; \
			cd $$dir && wire && cd ..; \
		else \
			echo "Warning: $$dir/wire.go not found, skipping"; \
		fi \
	done

# show help
help:
	@echo ''
	@echo 'Usage:'
	@echo ' make [target]'
	@echo ''
	@echo 'Targets:'
	@awk '/^[a-zA-Z\-\_0-9]+:/ { \
	helpMessage = match(lastLine, /^# (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")-1); \
			helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
			printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help